package com.gzgy.ustore.api.web.controller.webservice;


import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.service.DvcGroupService;
import com.gzgy.ustore.api.util.ResultUtil;
import com.gzgy.ustore.api.web.controller.BaseController;
import com.ustore.model.DvcGroup;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.SQLException;

@Controller
public class DvcGroupController extends BaseController {

    @Autowired
    private DvcGroupService dvcGroupService;

    @RequestMapping(value = "dvcGroupTree" ,method = RequestMethod.POST)
    public String dvcGroupTree(ModelMap modelMap,String userId,String session,String companyId){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(dvcGroupService.dvcGroupTree(NumberUtils.toInt(companyId))));
        return  SHOW_JSON_NAME;
    }

    @RequestMapping(value = "dvcGroupList" ,method = RequestMethod.POST)
    public String dvcGroupList(ModelMap modelMap,String userId,String session,String companyId){

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        modelMap.put("message", ResultUtil.getResultJson(dvcGroupService.dvcGroupList(NumberUtils.toInt(companyId))));
        return  SHOW_JSON_NAME;
    }

    @RequestMapping(value = "dvcGroupOperation",method = RequestMethod.POST)
    public String dvcGroupOperation(ModelMap modelMap,String userId,String session,String companyId,String operationFlag,String dvcGroupId,String dvcGroupName,String remark) throws SQLException {

        Assert.hasText(companyId,"companyId " + I18nUtil.getMessage(-2,null,"zh_CN"));
        Assert.hasText(operationFlag,"operationFlag " + I18nUtil.getMessage(-2,null,"zh_CN"));

        if(operationFlag.equals(DvcGroup.DVC_GROUP_OPERATION_ADD)){
            Assert.hasText(dvcGroupName,"dvcGroupName " + I18nUtil.getMessage(-2,null,"zh_CN"));
        }else {
            Assert.hasText(dvcGroupId,"dvcGroupId " + I18nUtil.getMessage(-2,null,"zh_CN"));

        }
        String result = dvcGroupService.dvcGroupOperation(NumberUtils.toInt(dvcGroupId),NumberUtils.toInt(companyId),operationFlag,dvcGroupName,remark,userId);
        modelMap.put("message",ResultUtil.getResultJson(result));
        return SHOW_JSON_NAME;
    }

}
